#include "q3dsurfacedemo01.h"

#include <QtDataVisualization/Q3DSurface>
#include <QtDataVisualization/QSurfaceDataArray>
#include <QtDataVisualization>
#include <QtMath>

#include "ui_q3dsurfacedemo01.h"

Q3DSurfaceDemo01::Q3DSurfaceDemo01(QWidget *parent)
    : QWidget(parent), ui(new Ui::Q3DSurfaceDemo01) {
    ui->setupUi(this);

    surface_ = new Q3DSurface();

    ui->verticalLayout->addWidget(QWidget::createWindowContainer(surface_));

    addSeries();
}

Q3DSurfaceDemo01::~Q3DSurfaceDemo01() { delete ui; }

QSurfaceDataArray *Q3DSurfaceDemo01::createData() {
    QSurfaceDataArray *array = new QSurfaceDataArray;

    for (int x = -5; x <= 5; x++) {
        QSurfaceDataRow *row = new QSurfaceDataRow();
        for (int y = -5; y <= 5; y++) {
            float z = qSqrt(x * x + y * y);
            row->append(QVector3D(x, y, z));
        }
        array->append(row);
    }
    return array;
}

void Q3DSurfaceDemo01::addSeries() {
    auto array  = createData();
    auto series = new QSurface3DSeries();
    series->dataProxy()->resetArray(array);
    surface_->addSeries(series);
    surface_->axisX()->setAutoAdjustRange(true);
    surface_->axisX()->setTitleVisible(true);
    surface_->axisY()->setAutoAdjustRange(true);
    surface_->axisZ()->setAutoAdjustRange(true);
    surface_->scene()->activeCamera()->setCameraPreset(
        Q3DCamera::CameraPresetDirectlyAbove);
}
